﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Web;
using System.Web.Helpers;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using Nb.Common.Logs;
using Nb.Common.Web.Mvc;

namespace Nb.Web
{
    public class NbMvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            ////setup nhibernate
            //IDatabaseConfig setupConfigHelper = new DatabaseConfig();
            //bool recreateDb = setupConfigHelper.GetRecreateDb();
            //string dbName = setupConfigHelper.GetDbName();
            ////开发时用（数据库由程序自动生成）
            //NHibernateConfig.SetupNHibernate(dbName, recreateDb);

            //ioc setup
            NinjectConfig.SetupCore();
            NinjectConfig.SetupAdapterResolvers();
            NinjectConfig.LoadModules(false);


            var logger = this.GetLogger();
            logger.Debug("test");

            ////初始化准备完毕后，才能出发此事件
            //DomainEventBus.Raise(new ApplicationStartingEvent());

            //#region 延迟绑定和加载ISession的策略解决了此问题
            ////修正 Illegally attempted to associate a proxy with two open Sessions 的bug
            ////原因是Application_Start()开始于Application_BeginRequest()之前
            ////Logger.Debug("ApplicationStartBindSession()");
            //////SessionBind();
            //#endregion

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

            ////放在异常处理Filter之后，用于显示特定的异常信息
            //LicenseConfig.SetupLicense();

            ////webapi的路由注册
            //GlobalConfiguration.Configure(WebApiConfig.Register);

            //Mvc的路由注册
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            //////Area级别的注册
            //NbAreaRegistration.RegisterAllAreasInvoke();

            ////行政区代码的【内存仓储】实现
            //DistrictConfig.InitDistrict();

            ////读取菜单和权限设置
            ////【内存仓储】的实现
            //MetaConfig.LoadMetas();

            ////预置数据初始化(SetupInitDataBase<District>应由外部保证是干净的数据库，但不适用于【内存仓储】，因为内存仓储需要永远加载，跟是否重新生成数据库无关)
            //var systemRunningGuard = CoreServiceProvider.LocateService<SystemRunningGuard>();
            //systemRunningGuard.Demand();

            ////准备Setup数据
            //if (recreateDb)
            //{
            //    //这会导致Web重启
            //    ChangeRecreateDbToFalse();
            //}

            //一些重要的设置项
            AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Name;

            //themeable view engines
            ThemeConfig.RegisterGlobalEngines(ViewEngines.Engines);
        }
    }
}
